_G['COMMANDS']={
--~     心跳
	heart= function(socket,data)
		io.write('receive heart------------------------------\n')
		if sockets[socket] then
		   local user=users[sockets[socket]]
		   user['online_time']=math.ceil((CGetTime()-user['login_time'])/1000)
		end
		return 0
	end,
	-- 在线时间
	getonlinetime= function(socket,data)
		local ret={c='getonlinetime',m=''}
		if data['u'] then
			if  users[data['u']] ~=nil then
				 ret['m']='getonlinetime_ok'
				 ret['online_time']=math.ceil((CGetTime()-users[data['u']]['login_time'])/1000)
			else
				 ret['m']='user_not_online'
			end

		else
		   ret['m']='no_user_param'

		end

		CNetSend(socket,ret)
	end,
--~ 初始化模型信息
	initmodel=function(socket,data)
         if data['u']~=nil and users[data['u']]~=nil then
		     local user=users[data['u']]
			  user['cloth']=data['cloth']
              user['weapons']=data['weapons']
			  user['rider']=data['rider']
			  user['title']=data['title']
			  user['locked']=data['locked'] or 0
			  user['chat_lock']=data['chat_lock'] or 0
			  if user['chat_lock']~='0' or user['locked']~='0' then
				 blacklist[user['u']]=user['u']
			  end
			  users[data['u']]=user
			  _G['COMMANDS'].addmapuser(user['socket_id'],user,maps[user['scene_id']],'login')
			CNetSend(socket,{c='initmodel',m='initmodel_ok'})
		 else
             CNetSend(socket,{c='initmodel',m='user_not_online'})
		 end

	end,
--~ 更新模型信息
	updatemodel=function(socket,data)
	     if data['u']~=nil and users[data['u']]~=nil then
		     local user=users[data['u']]
			  user['cloth']=data['cloth'] or user['cloth']
              user['weapons']=data['weapons'] or user['weapons']
			  user['rider']=data['rider'] or user['rider']
			  user['title']=data['title'] or user['title']
			  users[data['u']]=user
			  local ret={c='updatemodel',u=user['u'],baseBodyAvatarID=user['baseBodyAvatarID'],rid=user['rid'],cloth=user['cloth'],weapons=user['weapons'],rider=user['rider'],title=user['title']}
			  if maps[user['scene_id']]~=nil then
			         for x,y in pairs(maps[user['scene_id']]) do
						CNetSend(users[y]['socket_id'],ret)
					 end
			  end

			CNetSend(socket,{c='updatemodel',m='updatemodel_ok'})
		 else
             CNetSend(socket,{c='updatemodel',m='user_not_online'})
		 end
	end,

--~     登录
	login = function(socket,data,ip)
		if server_status==0 and  systemusers[data['u']]==nil then
		    local ret={c='login',m='server_shutdown'}
			CNetSend(socket,ret)
			return 0
		end

		if  not data['u'] then
			local ret={c='login',e=1,m='no_uname'}
			CNetSend(socket,ret)
			return 0
		end
		if  not data['key'] then
			local ret={c='login',e=2,m='no_key'}
			CNetSend(socket,ret)
			return 0
		end
		if systemusers[data['u']]~=nil then
			if not data['adminkey'] then
				 local ret={c='login',e=2,m='no_adminkey'}
				CNetSend(socket,ret)
				return 0
			 end

			if adminKey ~= data['adminkey'] then
				 print(adminKey)
				 print(data['adminkey'])
				 local ret={c='login',e=3,m='wrong_adminkey'}
				 CNetSend(socket,ret)
				 return 0
			end
		end

		local key=CMd5(systemKey..data['u'])
		if key~=data['key'] then
			print(key..'^^^^^'..data['key'])
			CNetSend(socket,{c='login',e=1,m='wrong_key'})
			return 0
		end
		if not data['rid'] then
			local ret={c='login',e=4,m='no_rid'}
			--print('no_rid')
			--CNetSend(socket,ret)
			--return 0
		end

		if users[data['u']]~=nil and users[data['u']]['socket_id']~=socket then
			--顶号通知前一个账号退出
			local ret={c='relogin',s=false,m='be_relogin'}
			local old_user=users[data['u']]
			CNetSend(users[data['u']]['socket_id'],ret)
		    --清除被顶号角色的地图信息
			if  maps[old_user['scene_id']][data['u']] ~= nil then
				 maps[old_user['scene_id']][data['u']] = nil
			end
			data['relogin']=true
			reloginusers[users[data['u']]['socket_id']]=users[data['u']]['socket_id']
		end
		data['socket_id']=socket
		data['ip']=ip
		data['teamlist']={}
		data['last_world_chat_time']=0
		data['online_time']=0
		data['x']=data['x'] or 0
		data['y']=data['y'] or 0
		data['chat_lock']=data['chat_lock'] or 0
		data['extra']=data['extra'] or ''
		data['login_time']=CGetTime()
		sockets[socket]=data['u']
		users[data['u']]=data
--~ 					地图信息
		if data['scene_id']~= nil then
		   local sceneid=data['scene_id']
		   if maps[sceneid] == nil then maps[sceneid]={} end
			maps[sceneid][data['u']]=data['u']

		end
--~                    军团(帮派信息)
		if data['league_id']~=nil and data['league_id']>=0 then
			local leagueid=data['league_id']
			if leagues[leagueid]==nil then leagues[leagueid]={} end
			leagues[leagueid][data['u']]=data['u']
		end
		if data['chat_lock']~=nil and data['chat_lock']~=0  then
			blacklist[data['u']]=data['u']
		end

		local ret={c='login',s=true,m='login_ok'}
		CNetSend(socket,ret)
		if type(offlines[data['u']])=='table' and next(offlines[data['u']])~=nil then
			for x,y in ipairs(offlines[data['u']]) do
				CNetSend(socket,{c='chatoffline',m=y['m'],from=y['from'],t=y['t']})
			end
			offlines[data['u']]={}
		end
		return 0
	end,
--~     登出
	logout = function(socket,data)
		io.write('receive logout------------------------------\n')
		if  not data['u'] then
			local ret={c='logout',s=false,m='no_uname'}
			CNetSend(socket,ret)
		end
		if  users[data['u']] ~=nil then
			users[data['u']]=nil
			local ret={c='logout',s=true,m='logout_ok'}
			CNetSend(socket,ret)
			return 0
		else
			local ret={c='logout',s=false,m='user_not_found'}
			CNetSend(socket,ret)
		end

		return 0
	end,
--~ 	加入某个阵营
	joincamp = function(socket,data)
		local user=users[sockets[socket]]
		local ret={c='joincamp',m='joincamp_ok',t=data['t']}
		 if user~=nil and user['camp']==nil then
				local camp=data['camp'] or nil
				if camp~=nil then
					if camps[camp]==nil then
					   camps[camp]={}
					end
					users[sockets[socket]]['camp']=camp
					camps[camp][user['u']]=socket
				else
					ret['m']='no_camp_param'
				end
		 else
			 ret['m']='already_in_camp'
		 end
		 CNetSend(socket,ret)
		 return 0
	end,
--~  退出阵营
	quitcamp=function(sokcet,data)
		local user=users[sockets[socket]]
		local ret={c='quitcamp',m='quitcamp_ok',t=data['t']}
		if user~=nil and user['camp']~=nil then
			if camps[user['camp']]~=nil and camps[user['camp']][user['u']]~=nil then
				camps[user['camp']][user['u']]=nil
				users[sockets[socket]]['camp']=nil
--~ 					所有人都退出解散阵营
				if next(camps[user['camp']])==nil then
					 camps[user['camp']]=nil
				end
			else
				ret['m']='camp_not_exists'
			end
		else
			ret['m']='not_in_camp'
		end
		CNetSend(socket,ret)
		return 0
	end,
--~  切换阵营
     changecamp=function(socket,data)
		local ret={c='changecamp',m='changecamp_ok',t=data['t']}
		local user=users[sockets[socket]]
		if user['camp']~=nil then
			local camp=data['camp']
			if camps[user['camp']]~=nil and camps[camp]~=nil then
				users[sockets[socket]]['camp']=camp
				camps[user['camp']][user['u']]=nil
				if next(camps[user['camp']])==nil then
					 camps[user['camp']]=nil
				end
				camps[camp][user['u']]=socket
			else
				ret['e']=2
				ret['m']='camp_not_exists'
			end
		else
		    ret['e']=1
			ret['m']='not_in_camp'
		end
		CNetSend(socket,ret)
		return 0
	 end,
--~   阵营聊天
     chatcamp=function(socket,data)
        local ret={c='chatcamp',m=data['m'],t=data['t']}
		local user=users[sockets[socket]]
		if user['camp']~=nil then
			 if camps[user['camp']]~=nil and next(camps[user['camp']])~=nil then
                 for u,s in camps[user['camp']] do
					CNetSend(s,ret)
				 end
			 else
			 ret['e']=2
			 ret['m']='camp_not_exists'
			 CNetSend(socket,ret)
			 end
		else
		   ret['e']=1
		   ret['m']='not_in_camp'
		    CNetSend(socket,ret)
		end
		return 0
	 end,
--~  获取某个角色信息
	getuserinfo=function(socket,data)
		if data['u']~=nil and users[data['u']]~=nil then
			CNetSend(socket,{c='getuserinfo',m='getuserinfo_ok',info=users[data['u']]})
		elseif data['u']==nil then
		   CNetSend(socket,{c='getuserinfo',m='no_username_found',list=ret})
		else
		   CNetSend(socket,{c='getuserinfo',m='user_not_online',list=ret})
		end
		return 0
	end,
--~ 	获取一些角色信息
	getusersinfo=function(socket,data)
	    local ret={c='getusersinfo',m='getusersinfo_ok',list={},t=data['t']}
		if data['userlist']~=nil and next(data['userlist'])~=nil then
            for x,y in pairs(data['userlist']) do
				ret['list'][x]=users[y]
			end
		end
		CNetSend(socket,ret)
		return 0
	end,
--~ 	获取所有地图角色信息 管理用
	getallmapusers=function(socket,data)
	    local ret={}
		if next(maps)~=nil then
           for x,y in pairs(maps) do
			  if type(y)=='table' and next(y)~=nil then
			      local sx=tostring(x)
			      ret[sx]={}
			      for z,w in pairs(y) do
				   table.insert(ret[sx],w)
				  end
			  end
		   end
		   CNetSend(socket,{c='getallmapusers',m='getallmapusers_ok',t=data['t'],list=ret})
		end
		return 0
	end,
-- 在地图中移动
    mapmove=function(socket,data)
         if sockets[socket]~=nil then
			 local user=users[sockets[socket]]
			  if user['scene_id']~=nil and maps[user['scene_id']]~=nil and type(maps[user['scene_id']])=='table'  then
					local map=maps[user['scene_id']]
					local ret={c='mapmove',x=data['x'],y=data['y'],m='mapmove_ok',t=data['t'],rid=user['rid']}
					users[sockets[socket]]['x']=data['x']
					users[sockets[socket]]['y']=data['y']
					for x,y in pairs(map) do
						if users[y]~=nil  then
							CNetSend(users[y]['socket_id'],ret)
						end
					end
			  end
--~ 			CNetSend(socket,{c='mapmove',m='mapmove_ok',t=data['t']})
		 end
	end,
--~ 更新玩家额外数据 坐骑宠物等
    updateextra=function(socket,data)
	  local ret={c='updateextra',m='updateextra_ok',t=data['t']}
	  if sockets[socket]~=nil and data['extra']~=nil then
			local user=users[sockets[socket]]
			if user['scene_id']~=nil and maps[user['scene_id']] ~=nil then
			    user['extra']=data['extra']
				users[sockets[socket]]=user
				local map=maps[user['scene_id']]
				local update={c='updateextra',rid=user['rid'],u=sockets[socket],extra=user['extra'],t=data['t']}
				for x,y in pairs(map) do
					if users[y]~=nil and users[y]['socket_id']~=socket then
						CNetSend(users[y]['socket_id'],update)
					end
				end
			end
	  else
		ret['m']='no_extra'
	  end
	  CNetSend(socket,ret)
	end,
--~ 	地图切换
	changemap=function(socket,data)
		local current_check=true
        if data['from']~= nil and data['to']~=nil then
		   local from,to,t=data['from'],data['to'],data['t']
		    if from==to then
				CNetSend(socket,{c='changemap',e=1,m='the_same_map',t=data['t']})
				current_check=false
			end
		   if   current_check ==true and maps[from]~=nil and maps[from][sockets[socket]]~=nil then
               maps[from][sockets[socket]]=nil
		   elseif current_check ==true then
               CNetSend(socket,{c='changemap',e=2,m='you_are_not_in_from_map',t=data['t']})
			   current_check=false
		   end
		   if maps[to]==nil then  maps[to]={} end
		   if current_check==true and type(maps[to]) =='table' then
		       maps[to][sockets[socket]]=sockets[socket]
			   users[sockets[socket]]['scene_id']=to
			   _G['COMMANDS'].removemapuser(socket,users[sockets[socket]],maps[from],data['t'])
			   _G['COMMANDS'].addmapuser(socket,users[sockets[socket]],maps[to],data['t'])
			   CNetSend(socket,{c='changemap',e=0,m='changemap_ok',t=data['t']})
		   end

		else
		    CNetSend(socket,{c='changemap',e=0,m='need_from_and_to'})
		end
		return 0
	end,
--~ 这个接口前端不能调用
	addmapuser=function(socket,user,map,t)
		local ret={c="addmapuser",user=user,t=t}
		for x,y in pairs(map) do
			if users[y]['socket_id']~=nil and users[y]['socket_id']~=socket then
			    CNetSend(users[y]['socket_id'],ret)
			end
		end
	end,
--~ 这个接口前端不能调用
    removemapuser=function(socket,user,map,t)
		local ret={c="removemapuser",user=user,t=t}
		for x,y in pairs(map) do
			if users[y]['socket_id']~=nil and users[y]['socket_id']~=socket then
			    CNetSend(users[y]['socket_id'],ret)
			end
		end
	end,
--~ 	系统信息
	system=function(socket,data)
       if data['type']~=nil  then
--~             发送给特定玩家
			 if data['type']=='user' then
				if data['u']~=nil then
                    if users[data['u']]~=nil then
						CNetSend(users[data['u']]['socket_id'],{c='system',e=0,t=data['t'],type='system',m=data['m']})
					else
						CNetSend(socket,{c='system',e=1,m='user_not_online'})
					end
				else
                    CNetSend(socket,{c='system',e=2,m='no_uname_found'})
				end
--~             发送给所有人
			 elseif data['type']=='all' then
				for x,y in pairs(users) do
                    if y['socket_id']~=nil and y['socket_id']~=socket then
						CNetSend(y['socket_id'],{c='system',e=3,type='system',t=data['t'],m=data['m']})
					end
				end
--~             发送给某个队伍
			 elseif data['type']=='team' then
				if data['teamid'] ~=nil then
					if teamlist[data['teamid']]~=nil then
						local team=teamlist[data['teamid']]
						for x,y in pairs(team) do
							CNetSend(users[x]['socket_id'],{c='system',t=data['t'],type='system',m=data['m']})
						end
					else
						CNetSend(socket,{c='system',e=3,m='team_not_exists'})
					end
				else
                    CNetSend(socket,{c='system',e=4,m='no_teamid_found'})
				end
--~             自定义玩家列表
			 elseif data['type']=='list' then
                 if data['list']~=nil and type(data['list'])=='table' then
					for x,y in pairs(data['list']) do
						if users[y]~=nil then
							CNetSend(users[y]['socket_id'],{c='system',type='system',t=data['t'],m=data['m']})
						end
					end
				 end
			 else
                 CNetSend(socket,{c='system',t=data['t'],e=5,m='no_such_type'})
		     end
		   CNetSend(socket,{c='system',t=data['t'],m='system_ok'})
		else
			CNetSend(socket,{c='system',t=data['t'],e=6,m='need_type'})
	   end
	   return 0
 	end,
--~     获取全局角色列表
	getusers=function(socket,data)
		CNetSend(socket,{c='getusers',users=users,m='getusers_ok'})
		return 0
	end,
	getallusernames=function(socket,data)
	    local ret={c="getallusernames",list={},m="getallusernames_ok"}
		for i,name in pairs(sockets) do
		     table.insert(ret['list'],sockets[i])
		end
		CNetSend(socket,ret)
		return 0
	end,
--~ 获取所有在线玩家ID
    getalluserids=function(socket,data)
	    local ret={c="getalluserids",list={},m="getalluserids_ok"}
		for i,user in pairs(users) do
		     table.insert(ret['list'],user['rid'])
		end
		CNetSend(socket,ret)
		return 0
	end,
--~     在线人数
	getonlinecount=function(socket,data)
		local count=0
		 for x,y in pairs(users) do
			  count=count+1
		 end
		CNetSend(socket,{c='getonlinecount',count=count,m='getonlinecount_ok'})
		return 0
	end,
--~     当前地图角色列表
	getmapusers=function(socket,data)
	   data['scene_id']=data['scene_id'] or users[sockets[socket]]['scene_id']
       if data['scene_id'] ~= nil then
			local sceneid=data['scene_id']
			if maps[sceneid]~=nil  and type(maps[sceneid]) == 'table' then
			    local ret={c='getmapusers',s=true,m='getmapusers_ok',list={}}
			    for x,y in pairs(maps[sceneid]) do
                    table.insert(ret['list'],users[y])
			    end
				CNetSend(socket,ret)
			else
			    local ret={c='getmapusers',m='no_user_in_such_scene',list={}}
				CNetSend(socket,ret)
			end
	   else
          CNetSend(socket,{c='getmapusers',m="sceneid_not_found",list={}})
	   end
	   return 0
	end,
--~ 	当前地图聊天
    chatmap=function(socket,data)
	    local from = data['from'] or sockets[socket]
		local user=users[sockets[socket]]
		if user~=nil then
		    local map=maps[user['scene_id']]
			if type(map) =='table' then
			    for x,y in pairs(map) do
					CNetSend(users[y]['socket_id'],{c='chatmap',t=data['t'],from=from,m=data['m']})
				end
			end
		end
		return 0
	end,
	--任务通知
	mission=function(socket,data)

	local ret={c='mission',u=data['u'],t=data['t'],m="mission_ok",mid=data['mid']}
		if  data['u']~=nil then
			if users[data['u']]~=nil then
			    CNetSend(users[data['u']]['socket_id'],ret)
				CNetSend(socket,ret)
			else
			    ret['m']='user_not_online';
				ret['e']=2
				CNetSend(socket,ret)
			end
		else
		ret['m']="no_user_param";
			CNetSend(socket,ret)
		end
		return 0
	end,
	--小喇叭
	speeker=function(socket,data)

		local ret={c='speeker',u=data['u'],g=data['g'],m=data['m'],t=data['t']}
		for name,user in pairs(users) do
			if user['socket_id']~=nil and user['socket_id']~=socket then
					 CNetSend(user['socket_id'],ret);
			end
		end
		ret['m']='speeker_ok'
		CNetSend(socket,ret)
		return 0
	end,
--~ 	世界发言
    chatworld=function(socket,data)
		local from= data['from'] or sockets[socket]
		if from~=nil then
			local interval=os.time()-users[from]['last_world_chat_time']
			if data['m']~=nil and interval>=worldChatInterval then
				for name,user in pairs(users) do
					if user['socket_id']~=nil then
					  CNetSend(user['socket_id'],{c='chatworld',t=data['t'],from=from,to='all',m=data['m']})
					end
				end
			users[from]['last_world_chat_time']=os.time()
			elseif data['m']~=nil and interval<worldChatInterval then
				CNetSend(socket,{c='chatworld',e=1,from=from,t=data['t'],to='all',m='world_chat_interval_limit'})
		   end
		else
		    CNetSend(socket,{c='chatworld',m='need_login'})
	   end
	   return 0
	end,
--~ 全局广播
	broadcast=function(socket,data)
			if data['m']~=nil then
			    local msg={c='broadcast',t=data['t'],m=data['m']}
				for name,user in pairs(users) do
					if user['socket_id']~=nil and socket~=user['socket_id'] then
					  CNetSend(user['socket_id'],msg)
					end
				end
			end
			CNetSend(socket,{c='broadcast',t=data['t'],m='broadcast_ok'})
			return 0
	end,
--~ 	好友聊天
	chatfriend=function(socket,data)
		if data['u']~=nil then
			if users[data['u']]~=nil then
				CNetSend(users[data['u']]['socket_id'],{c='chatfriend',t=data['t'],from=from,to=data['u'],m=data['m']})
				CNetSend(socket,{c='chatfriend',t=data['t'],m='send_chatfriend'})
			else
				CNetSend(socket,{c='chatfriend',t=data['t'],e=1,m='friend_not_online'})
			end
		else
			CNetSend(socket,{c='chatfriend',t=data['t'],e=1,m='no_username_found'})
		end
		return 0
	end,
--~ 	私聊
    chatprivate=function(socket,data)
		local from= data['from'] or sockets[socket]
        if data['u']~=nil then
                if users[data['u']]~=nil then
			        CNetSend(users[data['u']]['socket_id'],{c='chatprivate',t=data['t'],from=from,to=data['u'],m=data['m']})
					CNetSend(socket,{c='chatprivate',t=data['t'],m='send_chatprivate_ok'})
				else
                    CNetSend(socket,{c='chatprivate',t=data['t'],e=1,m='private_friend_not_online'})
				end
			else
				CNetSend(socket,{c='charprivate',t=data['t'],e=1,m='no_username_found'})
			end
			return 0
	end,
--~ 	离线消息
	chatoffline=function(socket,data)
	local from= data['from'] or sockets[socket]
		if data['u']~=nil then
			local uname=data['u']
--~ 			在线直接发送
			if users[uname]~=nil then
				CNetSend(users[uname]['socket_id'],{c='chatoffline',t=data['t'],from=from,m=data['m']})
			else
                if offlines[uname]==nil then offlines[uname]={} end
				local sum=0
				for i,j in ipairs(offlines[uname]) do
				    sum=sum+1
				end
				if sum <100 then
					table.remove(offlines[uname],0)
					table.insert(offlines[uname],{from=from,t=data['t'],m=data['m']})
					CNetSend(socket,{c='chatoffline',t=data['t'],m='chatoffline_ok'})
				else
					CNetSend(socket,{c='chatoffline',t=data['t'],e=1,m='more_than_100'})
				end
			end
		else
			CNetSend(socket,{c='chatoffline',t=data['t'],m='no_uname_found'})
		end
		return 0
	end,
--~ 	创建队伍
	createteam=function(socket,data)
		if data['teamid']~=nil then
		   local teamid=data['teamid']
		   local exists=false
		   if teamlist[teamid]~=nil  and next(teamlist[teamid])~=nil then
		         CNetSend(socket,{c='createteam',e=1,t=data['t'],m='team_already_exists'})
				 exists=true
		   end
		   if exists==false then
			   local team={}
			   team[sockets[socket]]=users[sockets[socket]]
			   users[sockets[socket]]['teamid']=teamid
               teamlist[teamid]=team
			   CNetSend(socket,{c='createteam',t=data['t'],m="createteam_ok"})
		   end
		else
			CNetSend(socket,{c='createteam',t=data['t'],e=2,m='no_teamid_found'})
		end
		return 0

	end,
--~ 移交队长
    changeleader=function(socket,data)

	end,
--~ 	解散队伍
	destroyteam=function(socket,data)
--~ 		local uname= data['u'] or sockets[socket]
		if data['teamid']~=nil then
		   local teamid=data['teamid']
		   print(teamid)
		   local exists=false
		   if teamlist[teamid]~=nil and type(teamlist[teamid])=='table' then
               for x,y in pairs(teamlist[teamid]) do
			       users[x]['teamid']=0
                   CNetSend(users[x]['socket_id'],{c="destroyteam",t=data['t'],teamid=teamid,m="team_destroyed"})
			   end
			   teamlist[teamid]=nil
			else

			CNetSend(socket,{c='destroyteam',t=data['t'],e=1,m='team_not_exists'})
		   end
		else
			CNetSend(socket,{c='destroyteam',t=data['t'],e=2,m='no_teamid_found'})
		end
		return 0
	end,
--~ 	踢队伍成员
	teamkick=function(socket,data)
	    if data['teamid']~=nil then
		    if data['u']~=nil then
                if data['u']~=sockets[socket] then
                     if teamlist[data['teamid']]~=nil and  teamlist[data['teamid']][data['u']]~=nil then
						  for x,y in pairs(teamlist[data['teamid']]) do
								CNetSend(socket,{c='teamkick',t=data['t'],e=0,teamid=data['teamid'],u=data['u'],m='team_be_kick'})
						  end
						 teamlist[data['teamid']][data['u']]=nil
						 CNetSend(socket,{c='teamkick',t=data['t'],e=0,m='teamkick_ok'})
					 else
						 CNetSend(socket,{c='teamkick',t=data['t'],e=4,m='team_not_exists_or_user_not_in_team'})
					 end

				else
					CNetSend(socket,{c='teamkick',t=data['t'],e=3,m='can_not_kick_your_self'})
				end

			else
				CNetSend(socket,{c='teamkick',t=data['t'],e=2,m='no_username_found'})
			end
		else
			CNetSend(socket,{c='teamkick',t=data['t'],e=1,m='no_teamid_found'})
		end
		return 0
	end,
--~ 	加入队伍
	jointeam=function(socket,data)
		local uname= data['u'] or sockets[socket]
		if data['teamid'] ~=nil then
		    local teamid=data['teamid']
			local inteam=false
			local user=users[uname]
			if user['teamid']~=nil and tonumber(user['teamid'])>0 then
			   inteam=true
			   CNetSend(socket,{c='jointeam',e=1,t=data['t'],m='you_are_in_team'})
			end
			if inteam==false and teamlist[teamid]~=nil and next(teamlist[teamid])~=nil then
			   local user=users[uname]
			   user.teamid=teamid
			   users[uname]=user
			   teamlist[teamid][uname]=user
			   CNetSend(socket,{c='jointeam',t=data['t'],e=0,m='jointeam_ok'})
               local team=teamlist[teamid]
--~ 				 广播给其他成员
				if team~=nil and next(team)~=nil then
					for x,y in pairs(team) do
					    if x~=uname then
						    CNetSend(users[x]['socket_id'],{c='jointeam',t=data['t'],u=uname,m='join_team_to_other'})
					    end
					end
				end
			elseif inteam == false then
				CNetSend(socket,{c='jointeam',t=data['t'],e=2,m='team_not_exists'})
			end
		else
			CNetSend(socket,{c='jointeam',t=data['t'],e=3,m='teamid_not_exists'})
		end
		return 0
	end,
--~ 	退出队伍
	quitteam=function(socket,data)
		local uname= data['u'] or sockets[socket]
		if data['teamid'] ~=nil then
		    local teamid=data['teamid']
			if teamlist[teamid]~=nil then
			    users[uname]['teamid']=0
			    teamlist[teamid][uname]=nil
			    local team=teamlist[teamid]
--~ 				 广播给其他成员
				if team~=nil and next(team)~=nil then
					for x,y in pairs(team) do
					    if x~=sockets[socket] then
						    CNetSend(users[x]['socket_id'],{c='quitteam',t=data['t'],u=uname,m='quit_team'})
						end
					end
				end
			    CNetSend(socket,{c='quitteam',t=data['t'],e=0,m='quitteam_ok'})
--~ 			   所有人都退出队伍解散
               if next(teamlist[teamid])==nil then
			       teamlist[teamid]=nil
			   end
			else
				CNetSend(socket,{c='quitteam',t=data['t'],e=1,m='team_not_exists'})
			end
		else
			CNetSend(socket,{c='quitteam',t=data['t'],e=2,m='teamid_not_exists'})
		end
		return 0
	end,
--~ 队伍聊天
	chatteam=function(socket,data)
		local uname= data['u'] or sockets[socket]
		if data['teamid'] ~=nil then
		    local teamid=data['teamid']
			if teamlist[teamid]~=nil and type(teamlist[teamid])=='table' then
			    local team=teamlist[teamid]
				for x,y in pairs(team) do
					local user=users[x]
					if user['socket_id']~=nil then
					    CNetSend(user['socket_id'],{c='chatteam',t=data['t'],from=uname,m=data['m']})
					end
				end
			else
				CNetSend(socket,{c='chatteam',t=data['t'],e=1,m='team_not_exists'})
			end
		else
			CNetSend(socket,{c='chatteam',e=2,t=data['t'],m='teamid_not_exists'})
		end
		return 0

	end,
--~ 	军团聊天
	chatleague=function(socket,data)
		local uname= data['u'] or sockets[socket]
		local user=users[uname]
		if  user~=nil and user['league_id'] ~=nil then
            local league=leagues[user['league_id']]
			if league~= nil and next(league)~=nil then
				 for x,y in pairs(league) do
					CNetSend(users[x]['socket_id'],{c='chatleague',t=data['t'],from=user['u'],m=data['m']})
				 end
			end
		else
			CNetSend(socket,{c='chatleague',t=data['t'],m='you_are_not_in_league'})
		end
		return 0
	end,
--~ 好友状态 查询好友是否在线
	friendstatus=function(socket,data)
	   local ret={c='friendstatus',t=data['t'],m='friendstatus_ok',friendlist={}}
       if data['friendlist']~=nil and next(data['friendlist'])~=nil then
		   for x,y in pairs(data['friendlist']) do
				if users[y]~=nil then
                    ret['friendlist'][y]=true
				else
					ret['friendlist'][y]=false
				end
		   end
		   CNetSend(socket,ret)
	   end
	   return 0
	end,
--~ 	添加到黑名单
	addblacklist=function(socket,data)
	   if systemusers[sockets[socket]]~=nil then
			if data['u']~=nil then
				local user=users[data['u']]
				if user~=nil then
					blacklist[data['u']]=1
					CNetSend(socket,{c='addblacklist',t=data['t'],m='addblacklist_ok'})
				else
					CNetSend(socket,{c='addblacklist',t=data['t'],m='user_not_found'})
				end
			else
				CNetSend(socket,{c='addblacklist',t=data['t'],m='no_username_found'})
			end
		else
			CNetSend(socket,{c='addblacklist',e=1,m='not_admin_user'})
		end
		return 0
	end,
--~ 	从黑名单中移除
	removeblacklist=function(socket,data)
	    if systemusers[sockets[socket]]~=nil then
			if data['u'] ~=nil then
				if blacklist[data['u']]~=nil then
					blacklist[data['u']]=nil
					CNetSend(socket,{c='removeblacklist',t=data['t'],m='removeblacklist_ok'})
				else
					CNetSend(socket,{c='removeblacklist',t=data['t'],m='user_not_in_blacklist'})
				end
			else
				CNetSend(socket,{c='removeblacklist',t=data['t'],m='no_username_found'})
			end
		else
			CNetSend(socket,{c='removeblacklist',e=1,m='not_admin_user'})
		end

		return 0

	end,
--~  踢人
	kickuser=function(socket,data)
	 if systemusers[sockets[socket]]~=nil then
        if data['u']~=nil then
		    if users[data['u']]~=nil then
				if data['u']~=sockets[socket] then
					CNetSend(users[data['u']]['socket_id'],{c='kickuser',t=data['t'],m='be_kick'})
					CNetSend(socket,{c='kickuser',t=data['t'],m='kickuser_ok'})
				else
				    CNetSend(socket,{c='kickuser',t=data['t'],m='can_not_kick_your_self'})
				end
			else
                CNetSend(socket,{c='kickuser',t=data['t'],m='user_not_found'})
  			end
		else
			CNetSend(socket,{c='kickuser',t=data['t'],m='no_username_found'})
		end
	else
		CNetSend(socket,{c='removeblacklist',e=1,m='not_admin_user'})
	end
		return 0
	end,
--  将某个IP加入禁止列表
	addreject=function(socket,data)
	     if data['ip']~=nil then
			rejectips[data['ip']]=1
			CNetSend(socket,{c='addreject',m='addreject_ok'})
		 else
            CNetSend(socket,{c='addreject',m='no_ip_param'})
		 end
		 return 0
	end,
--  将某个IP移出禁止列表
	delreject=function(socket,data)
         if data['ip']~=nil then
			rejectips[data['ip']]=nil
			CNetSend(socket,{c='delreject',m='delreject_ok'})
		 else
			CNetSend(socket,{c='delreject',m='no_ip_param'})
		 end
		 return 0
	end,
-- 发送服务器将要关闭通知
    shutdownnotice=function(socket,data)
		local ret={c='shutdownnotice',m='server_shutdownnotice'}
		 for x,y in pairs(users) do
			 if y['socket_id']~=nil and y['socket_id']~=socket then
			    CNetSend(y['socket_id'],ret)
			 end
		 end
		 return 0
	end,
--关闭服务器 其实不是真正关闭而是给所有人发送退出游戏消息
    shutdown=function(socket,data)
	     local ret={c='shutdown',m='server_shutdown'}
	     server_status=0
		 for x,y in pairs(users) do
			 if y['socket_id']~=nil   then
			    CNetSend(y['socket_id'],ret)
			 end
		 end

		 return 0
	end,
    serverstart=function(socket,data)
	     local ret={c='serverstart',m='server_start_ok'}
	     server_status=1
		 CNetSend(socket,ret)
		 return 0
	end,
--热重启 待实现
	hotrestart=function(socket,data)

	end

}
